/*
 * @Author: ChunLai
 * @Date: 2022-09-06 17:36:12
 * @LastEditTime: 2025-03-21 16:09:51
 * @Description: 日期类
 * @FilePath: \0.newInsure\src\methods\date.js
 */
/**
 * 获取生日的天数
 * @param {*} sDate2 "2022-01-12"
 * @returns
 */
export const getDetailBirthDay = (sDate2) => {
  if (!sDate2 || !isDateString(sDate2)) {
    console.error("Invalid birthDate format");
    return;
  }
  // 获取当前时间和给定日期的时间戳
  const currentTime = new Date().getTime();
  const targetTime = new Date(sDate2).getTime();
  // 计算时间差并转换为天数
  const diffInDays = Math.abs((targetTime - currentTime) / (1000 * 3600 * 24));
  // 返回天数差
  return Math.floor(diffInDays);
};

// 出生日期--最小值
export const getBirthDate = (age, year = 1, day = 1) => {
  var today = new Date();
  var tYear = today.getFullYear() - age - year;
  var tMonth = today.getMonth();
  var tDate = today.getDate() + day;
  var birthDate = new Date(tYear, tMonth, tDate);
  // 确保生成的日期不会超出有效范围
  if (isNaN(birthDate.getTime())) {
    throw new Error("生成的日期无效");
  }
  return birthDate;
};

// 出生日期--最大值
export const getDay = (day) => {
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = formatAdd0(today.getDate());
  return new Date(tYear, tMonth, tDate);
};

/**
 * 日期转字符串
 * @param {*} time 传入的时间
 */
export const dateToString = (time) => {
  const datenew = new Date(time);
  const year = datenew.getFullYear();
  const month = datenew.getMonth() + 1;
  const day = datenew.getDate();
  return `${year}-${formatAdd0(month)}-${formatAdd0(day)}`;
};

/**
 *
 * @param {*} startDate
 * @param {*} n n取负数表示n天/月/年前
 * @param {*} timeUnit  D"、"M"、"Y"分别表示天，月，年
 * @returns
 */

export const formatDateTime = (shijianchuo) => {
  let dateTime = new Date(shijianchuo),
    yDate = dateTime.getFullYear(),
    mDate = dateTime.getMonth() + 1,
    rDate = dateTime.getDate(),
    hTime = dateTime.getHours(),
    mTime = dateTime.getMinutes(),
    sTime = dateTime.getSeconds();
  return `${yDate}-${formatAdd0(mDate)}-${formatAdd0(
    rDate
  )} ${hTime}:${mTime}:${sTime}`;
};

// 获取证件有效期止期
export const countYearDayForAge = (birthDate, age, day) => {
  if (!birthDate) {
    return "";
  }
  let srt = getAfterDay(birthDate, age, "Y");
  srt = getAfterDay(srt, day, "D");
  return srt;
};
//year代表几年   startDate 代表开始日期
export const getAfterDay = (startDate, n, timeUnit) => {
  let curr_date = new Date(startDate.replace(/-/g, "/"));
  if (timeUnit === "D") {
    curr_date.setDate(curr_date.getDate() + n);
  } else if (timeUnit === "M") {
    curr_date.setMonth(curr_date.getMonth() + n);
  } else if (timeUnit === "Y") {
    curr_date.setFullYear(curr_date.getFullYear() + n);
  }
  var strYear = curr_date.getFullYear();
  var strMonth = curr_date.getMonth() + 1;
  var strDay = curr_date.getDate();
  return `${strYear}-${formatAdd0(strMonth)}-${formatAdd0(strDay)}`;
};
export const yuLuBirthOrder = (birthDate) => {
  // console.log('yuLuBirthOrder', birthDate);
  let res = false;
  const curDate = new Date(),
    curYear = curDate.getFullYear();
  // 被保人生日
  const userMD = birthDate.slice(5),
    uerDate = `${curYear}-${userMD}`,
    endDate = `${curYear}-12-31`;
  // 投保时间次日（月日）
  const tbStamp = new Date().getTime() + 1000 * 60 * 60 * 24 * 1,
    userStamp = new Date(uerDate.replace(/-/g, "/")).getTime(),
    endStamp = new Date(endDate.replace(/-/g, "/")).getTime(),
    sxrStamp = new Date(`${curYear + 1}-01-01`.replace(/-/g, "/")).getTime(); //预录单指定生效日
  // console.log(endStamp);
  if (userStamp >= tbStamp && userStamp <= endStamp) {
    res = true;
  }
  if (userStamp == sxrStamp) {
    res = true;
  }
  return res;
};

// 生日件: 生日当天投保，保费会按照小一岁计算
export const countBirthBill = (birthDate) => {
  // console.log('yuLuBirthOrder', birthDate);
  let res = false;
  const curDate = dateToString(new Date()),
    curMD = curDate.slice(5);
  // console.log(curMD);
  // 被保人生日
  const userMD = birthDate.slice(5);
  if (curMD == userMD) {
    res = true;
  }
  return res;
};

// 起保日期
export const getStartDate = (day) => {
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth() + 1;
  var tDate = today.getDate();
  return `${tYear}-${formatAdd0(tMonth)}-${formatAdd0(tDate)}`;
};

export const getEndDate = (dateStr = "") => {
  var today = new Date();
  if (dateStr) {
    today = new Date(dateStr);
  }
  var tYear = today.getFullYear() + 1;
  var tMonth = today.getMonth() + 1;
  var tDate = today.getDate();
  if (dateStr) {
    tDate = tDate - 1;
  }
  return `${tYear}-${formatAdd0(tMonth)}-${formatAdd0(tDate)}`;
};

export const isDateString = (dateString) => {
  const date = new Date(dateString);
  return !isNaN(date.getTime());
};

// 日期格式化补0
export const formatAdd0 = (num) => String(num).padStart(2, "0");
